package batch.premium.fulltrack;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.log4j.Logger;

import batch.common.report.ReportError;

public class gbr_fulltrack {

	private StringBuffer strError = new StringBuffer();
	final String TODAY = DateFormatUtils.format(new Date(), "yyyyMMdd");
	final String STRSUBJECT = "FullTrack Batch Error(" + TODAY + ")";

	public static void main(String[] args) throws Exception {

		gbr_fulltrack fulltrack = new gbr_fulltrack();

		Logger.getLogger("fulltrack").info("( * ) Start.");
		fulltrack.getXml();
		Logger.getLogger("fulltrack").info("( * ) End.");

	}

	private void getXml() {

		HttpURLConnection connection = null;
		// To make Data File by 'UTF-8'
		OutputStreamWriter oswFile = null;

		String sDir = "/home02/nas/gbr_pub/sm3/cma/fulltrack/";

		String[][] saURL = {
				// TOP Single Chart
				{
						sDir + "fulltrack_top_single.xml",
						"http://cs.samsungmobile.co.uk:8080/API/music?fetchType=track&filterby=2&orderby=2&startIndex=0&amount=8" }
				// Latest Single Chart
				,
				{
						sDir + "fulltrack_latest_single.xml",
						"http://cs.samsungmobile.co.uk:8080/API/music?fetchType=track&filterby=6&orderby=2&startIndex=0&amount=8" }
				// Top Album Charts
				,
				{
						sDir + "fulltrack_top_album.xml",
						"http://cs.samsungmobile.co.uk:8080/API/music?fetchType=album&filterby=3&orderby=2&startIndex=0&amount=8" }
				// Latest Album Charts
				,
				{
						sDir + "fulltrack_latest_album.xml",
						"http://cs.samsungmobile.co.uk:8080/API/music?fetchType=album&filterby=0&orderby=2&startIndex=0&amount=8" }
				// Categories
				,
				{
						sDir + "fulltrack_categories.xml",
						"http://cs.samsungmobile.co.uk:8080/API/music?fetchType=genre&filterby=0&startIndex=0&amount=16" } };

		for (int i = 0; i < saURL.length; i++) {

			try {
				URL url = new URL(saURL[i][1]);

				connection = (HttpURLConnection) url.openConnection();
				connection.setConnectTimeout(10000);
				connection.setReadTimeout(100000);
				connection.setRequestMethod("POST");
				connection.setRequestProperty("Content-Type",
						"application/x-www-form-urlencoded");

				// connection.setRequestProperty("Content-Length", "0");

				connection.setRequestProperty("Content-Language", "de-DE");

				connection.setUseCaches(false);
				connection.setDoInput(true);
				connection.setDoOutput(true);

				DataOutputStream wr = new DataOutputStream(connection
						.getOutputStream());
				wr.flush();
				wr.close();

				BufferedReader in = new BufferedReader(new InputStreamReader(
						connection.getInputStream(), "UTF-8"));
				String inputLine;

				oswFile = new OutputStreamWriter(new FileOutputStream(
						saURL[i][0], false), "UTF-8");

				while ((inputLine = in.readLine()) != null) {
					oswFile.write(inputLine+"\n");
				}
				oswFile.close();

			} catch (Exception ex) {

				ex.printStackTrace();

				strError.setLength(0);
				strError
						.append(
								"---------------------  " + STRSUBJECT
										+ "  ---------------------\n")
						.append(
								ex.getMessage()
										+ "                                              \n")
						.append(
								"--------------------------------------------------------------- \n");
				Logger.getLogger("fulltrack").info(strError.toString());

				// processFail(CONTENTS_PATH); //Delete Images
				// processFail(log_path); //Delete Shell Files
				sendErrMail(strError);

			} finally {
				if (connection != null)
					connection.disconnect();
				if (oswFile != null)
					try {
						oswFile.close();
					} catch (Exception ex) {
						ex.printStackTrace();
					}
			}

		}

	}

	public void sendErrMail(StringBuffer strMsg) {
		ReportError.byEmail(STRSUBJECT, strMsg.toString());
	}

}